package com.stockz.equity.launcher;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;

import com.stockz.equity.charting.util.ContextUtil;
import com.stockz.equity.pattern.View;
import com.stockz.equity.ui.MainFrame;

public class Launcher {
	
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
		
		final JpaTemplate jpaTemplate = applicationContext.getBean(JpaTemplate.class);
		Runtime.getRuntime().addShutdownHook(new Thread(){
			@Override
			public void run() {
				jpaTemplate.execute(new JpaCallback<Object>(){

					public Object doInJpa(EntityManager em)
							throws PersistenceException {
						em.getTransaction().begin();
						em.flush();
						em.createNativeQuery("shutdown").executeUpdate();
						em.getTransaction().commit();
						return null;
					}
					
				});
			}
		});
		
		SwingUtilities.invokeLater(new Runnable(){
			public void run() {
				createAndShowGui(new MainFrame());
			}
		});
	}
	
	
	private static void createAndShowGui(final MainFrame frame){
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Insets screenInsets = frame.getToolkit().getScreenInsets(frame.getGraphicsConfiguration());         
        Rectangle screenSize = frame.getGraphicsConfiguration().getBounds();
        Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, 
                screenInsets.top + screenSize.y, 
                screenSize.x + screenSize.width - screenInsets.right - screenInsets.left,
                screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
        frame.setMaximizedBounds(maxBounds);
        Dimension maxDimension = new Dimension(maxBounds.width,maxBounds.height);
        frame.setMaximumSize(maxDimension);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setPreferredSize(maxDimension);
		frame.getMessageLabel().setPreferredSize(new Dimension(30,maxDimension.width));
		
		
		frame.addComponentListener(new ComponentAdapter(){
			@Override
			public void componentResized(ComponentEvent e) {
				for(java.awt.Component component: frame.getComponents()){
					component.validate();
				}
			}
		});
		
		JTabbedPane tabbedPane = frame.getTabbedPane();
		
		Map<String,View> viewMap = ContextUtil.getApplicationContext().getBeansOfType(View.class);
		List<View> orderedViewList = new ArrayList<View>();
		List<View> unorderedViewList = new ArrayList<View>();
		for(View view : viewMap.values()){
			if(view instanceof Ordered){
				orderedViewList.add(view);
			}else{
				unorderedViewList.add(view);
			}
		}
		OrderComparator.sort(orderedViewList);
		
		for(View view :orderedViewList){
			if(view instanceof JPanel){
				tabbedPane.addTab(view.getName(), (JPanel)view);
			}
		}
		for(View view :unorderedViewList){
			if(view instanceof JPanel){
				tabbedPane.addTab(view.getName(), (JPanel)view);
			}
		}
		tabbedPane.setSelectedIndex(0);
		frame.getContentPane().add(tabbedPane);
		frame.pack();
		frame.setVisible(true);
	}
	
}
